﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DLULearningNetwork.ActionFilterAttributes;
using DLULearningNetworkModel;
using DLULearningNetwork.Libs;

namespace DLULearningNetwork.Controllers
{
    public class FeedController : Controller
    {
        [RequireUserLogin]
        public ActionResult Detail([Bind(Prefix = "id")]Guid feedID)
        {
            Account currentUser = RequireUserLogin.LoginUser;
            Feed feed = Feed.SelectByFeedID(feedID);
            if (feed == null)
            {
                return PartialView("ErrorMessage", "Bài viết này đã bị xóa");
            }
            if (feed.Type_s != EnumFeedType.PostOnCourseWall.ToString()
                && feed.Type_s != EnumFeedType.PostOnWall.ToString())
            {
                return PartialView("ErrorMessage", "Mẫu tin này đã bị xóa");
            }
            if (feed.ActiveAccountID_u != currentUser.AccountID_u// post feed
                && feed.PassiveAccountID_u == currentUser.AccountID_u // feed thuoc tuong nguoi do
                )
            {
                ViewBag.AllowComment = true;
            }
            else {
                if (feed.Type_s == EnumFeedType.PostOnCourseWall.ToString() && feed.CourseReference.Value != null)
                {
                    if (feed.CourseReference.Value.AccountID_u == currentUser.AccountID_u)
                    {
                        ViewBag.AllowComment = true;
                    }
                    else
                    {
                        Enrollment enrollment = Enrollment.Single(currentUser.AccountID_u, feed.CourseID_u.Value);
                        if (enrollment == null || enrollment.Status_i != 1)
                        {
                            ViewBag.AllowComment = true;
                        }
                    }
                }
                else
                {
                    if (feed.Type_s == EnumFeedType.PostOnWall.ToString())
                    {
                        if (feed.ActiveAccountID_u == currentUser.AccountID_u)
                        {
                            ViewBag.AllowComment = true;
                        }
                        else
                        {
                            Friend friendRelation = Friend.Single(currentUser.AccountID_u, feed.PassiveAccountID_u.Value);
                            if (friendRelation != null && friendRelation.Status_i == (int)EnumFriendStatus.Accept)
                            {
                                ViewBag.AllowComment = true;
                            }
                        }
                        
                    }
                }
            }
            return View(feed);
        }

        [RequireUserLogin]
        [HttpPost]
        public ActionResult AddNewComment(Guid feedID, string content)
        {
            if (HttpUtility.HtmlDecode(content).Replace("\n", "").Replace("&nbsp", "").Length < 1)
            {
                return PartialView("ErrorMessage", "unsuccess/0");
            }
            if (HttpUtility.HtmlDecode(content).Replace("\n", "").Replace("&nbsp", "").Length > 5000)
            {
                return PartialView("ErrorMessage", "unsuccess/1");
            }
            Account currentUser = RequireUserLogin.LoginUser;
            Feed feed = Feed.SelectByFeedID(feedID);
            if (feed.Type_s != EnumFeedType.PostOnCourseWall.ToString() && feed.Type_s != EnumFeedType.PostOnWall.ToString())
            {
                return PartialView("ErrorMessage", "unsuccess");
            }
            if (feed.ActiveAccountID_u != currentUser.AccountID_u || feed.PassiveAccountID_u != currentUser.AccountID_u)
            { 
                //Kiem tra co la ban khong
                if (feed.Type_s == EnumFeedType.PostOnWall.ToString())
                {
                    Friend friendRelation = Friend.Single(currentUser.AccountID_u, feed.PassiveAccountID_u.Value);
                    if (friendRelation == null || friendRelation.Status_i != (int)EnumFriendStatus.Accept)
                    {
                        return PartialView("ErrorMessage", "unsuccess");
                    }
                }
                else
                {
                    if (feed.Type_s == EnumFeedType.PostOnCourseWall.ToString())
                    {
                        if (feed.CourseReference.Value.AccountID_u != currentUser.AccountID_u)
                        { 
                            Enrollment enrollment = Enrollment.Single(currentUser.AccountID_u,feed.CourseID_u.Value);
                            if (enrollment == null || enrollment.Status_i != 1)
                            {
                                return PartialView("ErrorMessage", "unsuccess");
                            }
                        }
                    }
                }
            }

            FeedComment fcmt = new FeedComment();
            fcmt.FeedCommentID_u = Guid.NewGuid();
            fcmt.FeedID_u = feedID;
            fcmt.ActiveAccount_u = currentUser.AccountID_u;
            fcmt.Content_s = content;
            fcmt.CreateDate_dt = DateTime.Now;
            if (FeedComment.Add(fcmt) == 1)
            {
                fcmt = FeedComment.SelectByFeedCommentID(fcmt.FeedCommentID_u);
                //Cập nhật lại ngày feed
                Feed.UpdateLastDate(feedID);

                if (feed.PassiveAccountID_u != currentUser.AccountID_u)
                {
                    Feed newFeed = new Feed();
                    newFeed.FeedID_u = Guid.NewGuid();
                    newFeed.ActiveAccountID_u = currentUser.AccountID_u;
                    newFeed.Type_s = EnumFeedType.ActivityComment.ToString();
                    newFeed.CreateDate_dt = DateTime.Now;
                    newFeed.LastUpdate_dt = DateTime.Now;
                    newFeed.CommentID = fcmt.FeedCommentID_u;
                    newFeed.Content_s = StringHelper.ShortcutContent(content, 30);
                    //Undone : transaction
                    Feed.Add(newFeed);
                }
                return PartialView("FeedComment", fcmt);
            }
            return PartialView("ErrorMessage", "unsuccess");
        }
    }
}
